package com.baizhi;
import com.baizhi.entity.CmfzLog;
import com.baizhi.mapper.CmfzLogMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.activemq.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import java.io.IOException;
@Service
public class MqBootListener {
    @Autowired
    private CmfzLogMapper cmfzLogMapper;
    /**
     *  @JmsListener 可以把一个方法变为 Mq的监听器方法
     *  方法的形参必须为 Message
     *  该方法不需要手动调用 在项目启动之后 SpringBoot会自动进行调用
     */
    @JmsListener(destination = "mq-boot")
    public void bootListener(Message message){
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println(textMessage.getText());
            /*
            * json字符串转对象
            *
            * ObjectMapper objectMapper = new ObjectMapper();
            * 将json字符串转为对象
            * */
            ObjectMapper objectMapper = new ObjectMapper();
            CmfzLog cmfzLog = objectMapper.readValue(textMessage.getText(), CmfzLog.class);
            System.out.println("转换后的cmfzlog对象:"+cmfzLog);
            cmfzLogMapper.insert(cmfzLog);
        }catch (JMSException | IOException e){
            e.printStackTrace();
        }
    }
}
